
package com.cmc.dao.hibernate;

import java.util.List;

import com.cmc.dao.hibernate.BaseDaoHibernate;
import com.cmc.model.WorkOverTime;
import com.cmc.dao.WorkOverTimeDao;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.MatchMode;
import org.springframework.orm.ObjectRetrievalFailureException;
import org.springframework.orm.hibernate3.HibernateCallback;

public class WorkOverTimeDaoHibernate extends BaseDaoHibernate implements WorkOverTimeDao {

    /**
     * @see com.cmc.dao.WorkOverTimeDao#getWorkOverTimes(com.cmc.model.WorkOverTime)
     */
    public List getWorkOverTimes(final WorkOverTime workOverTime) {
        //return getHibernateTemplate().find("from WorkOverTime");

        /* Remove the line above and uncomment this code block if you want 
           to use Hibernate's Query by Example API.*/
        if (workOverTime == null) {
            return getHibernateTemplate().find("from WorkOverTime");
        } else {
            // filter on properties set in the workOverTime
            HibernateCallback callback = new HibernateCallback() {
                public Object doInHibernate(Session session) throws HibernateException {
                    Example ex = Example.create(workOverTime).ignoreCase().enableLike(MatchMode.ANYWHERE);
                    return session.createCriteria(WorkOverTime.class).add(ex).list();
                }
            };
            return (List) getHibernateTemplate().execute(callback);
        }
    }

    /**
     * @see com.cmc.dao.WorkOverTimeDao#getWorkOverTime(Long id)
     */
    public WorkOverTime getWorkOverTime(final Long id) {
        WorkOverTime workOverTime = (WorkOverTime) getHibernateTemplate().get(WorkOverTime.class, id);
        if (workOverTime == null) {
            log.warn("uh oh, workOverTime with id '" + id + "' not found...");
            throw new ObjectRetrievalFailureException(WorkOverTime.class, id);
        }

        return workOverTime;
    }

    /**
     * @see com.cmc.dao.WorkOverTimeDao#saveWorkOverTime(WorkOverTime workOverTime)
     */    
    public void saveWorkOverTime(final WorkOverTime workOverTime) {
        getHibernateTemplate().saveOrUpdate(workOverTime);
    }

    /**
     * @see com.cmc.dao.WorkOverTimeDao#removeWorkOverTime(Long id)
     */
    public void removeWorkOverTime(final Long id) {
        getHibernateTemplate().delete(getWorkOverTime(id));
    }
}
